package cn.est.utils;

import cn.est.config.OssConfig;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @ClassName OssUtils
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2021/4/16 8:44
 * @Version 1.0
 **/
@Component
public class OssUtils {



    @Autowired
    private OssConfig ossConfig;


    /**
     * 获取ossclient客户端
     * @return
     */
    public OSS getOssClient(){

        OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccesskeyid(), ossConfig.getAccesskeysecret());
        return  ossClient;
    }


    public String uploadByStream(File file , String fileName){
        String strResult="";
        OSS ossclient =null;
        try {
            InputStream is=new FileInputStream(file);
            ossclient=getOssClient();
            PutObjectResult putObjectResult=ossclient.putObject(ossConfig.getBucketName(),fileName,is);
            strResult=putObjectResult.getETag();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e){
            if(ossclient!=null){
                ossclient.shutdown();
            }
        }
        return strResult;
    }


    public void deleteFile(String bucketName,String fileName){
        OSS ossClient=getOssClient();
        ossClient.deleteObject(bucketName,fileName);
        ossClient.shutdown();
    }

    // /est/model/aaa.jpg
    public void downloadFile(String bucketName,String fileName,HttpServletResponse response) throws IOException {
        OSS ossClient =getOssClient();


        //h获取OSSObject对象 阿里云 oss对象
        OSSObject ossObject=ossClient.getObject(bucketName,fileName);

        BufferedInputStream inputStream=new BufferedInputStream(ossObject.getObjectContent());
        BufferedOutputStream outputStream =new BufferedOutputStream(response.getOutputStream());

        int index =fileName.lastIndexOf("/");
        String newfileName=fileName.substring(index+1);


        //通知浏览器以附件的方式下载文件
        response.setHeader("Content-Disposition","attachment:filename="+URLEncoder.encode(newfileName,"utf-8"));

        //输出到浏览器
        byte[] car =new byte[1024];
        int L=0;
        while(  (L=inputStream.read(car))!=-1){
            System.out.println(new String(car));
                outputStream.write(car,0,L);
        }

        if(outputStream!=null){
            outputStream.close();
        }

        if(inputStream!=null){
            inputStream.close();
        }
        if(ossObject!=null)
        ossObject.close();
        if(ossClient!=null)
        ossClient.shutdown();
    }
}
